package com.javatest.springboot2basictest.processor;

import javax.validation.Validation;
import javax.validation.ValidatorFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2020-04-14 下午12:28
 */
@Component
public class BeanPostProcessorImpl implements BeanPostProcessor, Ordered {
    private static final Logger LOGGER = LoggerFactory.getLogger(BeanPostProcessorImpl.class);

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if ("juiceMaker".equals(beanName)) {
            LOGGER.warn("调用postProcessBeforeInitialization方法,[" + bean.getClass().getSimpleName() + "]对象" + beanName + "开始初始化");
        }
        ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if ("juiceMaker".equals(beanName)) {
            LOGGER.warn("调用postProcessAfterInitialization方法,[" + bean.getClass().getSimpleName() + "]对象" + beanName + "实例化完成");
        }
        return bean;
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
